package com.yimi.yinhepda.utils;

import android.widget.Toast;

import com.yimi.yinhepda.base.BaseApplication;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: lj
 * Date: 2019/5/6 11:17
 */
public class ToastUtil {

    private static Toast toast;
    private static Timer timer1;
    private static Timer timer2;

    public static void showToast(String msg) {
        if (BaseApplication.getContext() != null) {
            if (toast != null) {
                toast.cancel();
                toast = null;
            }
            if (timer1 != null) {
                timer1.cancel();
                timer1 = null;
            }
            if (timer2 != null) {
                timer2.cancel();
                timer2 = null;
            }
            toast = Toast.makeText(BaseApplication.getContext(), msg, Toast.LENGTH_LONG);
            showTimeToast(5000);
        }
    }

    public static void showTimeToast(final int cnt) {
        timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                if (toast != null) {
                    toast.show();
                }
            }
        }, 0, 3000);
        timer2 = new Timer();
        timer2.schedule(new TimerTask() {
            @Override
            public void run() {
                if (toast != null) {
                    toast.cancel();
                }
                if (timer1 != null) {
                    timer1.cancel();
                }
            }
        }, cnt);
    }

    public static void reset() {
        toast = null;
    }
}
